<?php
class Task{
	public $task_id;
	public $taskDB;
	public $extendsDB;
	public $progressDB;
	protected $error;
	public $arr;
	public function Task($task_id=''){
		$this->task_id=$task_id;
		$this->initialize();
	}
	protected function initialize(){
		$this->taskDB=M('task');//任务表
		$this->extendsDB=M('task_extends');//任务扩展表
		$this->progressDB=M('task_progress');//任务进程表
	}
	//测试
	public function view($view){
		$db=M('class_view');
		$data=$db->where(['class'=>__CLASS__])->getField('value');
		return $view->fetch('',$data);
	}
	//获取基本信息 
	public function getInfo($id='',$field=true){
		$id=''==$id?$this->task_id:$id;
		$info=$this->taskDB->where(['id'=>$id])->field($field)->find();
		return $info;	
	}
	public function getInfos($map=array()){
		if($map==array()){return false;}
		$infos=$this->taskDB->where($map)->select();
		return $infos;		
	}
	//通过Pid循环遍历数据
	public function getList($map,$deep=0){
		$re=$this->taskDB->where($map)->select();
		if($re!==false){
			$deep++;
			foreach($re as $v){
				$vo=$v;
				$vo['deep']=$deep;
				$this->arr[]=$vo;
				array_merge($this->arr,$this->getList(['pid'=>$v['id']],$deep));
			}
			return $this->arr;
		}
	}
	
	//获取树形结构的数组2
	public function selectall($map=array()){
		if($map==array()){return false;}
		$info=$this->taskDB->where($map)->select();
		
		$arr=[];
		if(false!==$info){
			foreach($info as $v){
				$arr[]=$this->getListall(['pid'=>$v['id']],$v);
			}
			return $arr;
		}else{
			return false;	
		}
	}
	//通过Pid循环遍历数据2
	public function getListall($map=array(),$arr=array()){
		$re=$this->taskDB->where($map)->select();
		if($re!==false){
			foreach($re as $v){
				$val=$this->getListall(['pid'=>$v['id']],$v);	
				$arr['children'][]=$val;
			}
			return $arr;
		}
	}
	
	//日常管理（增删改)
	public function insert($data=''){
		$data=''==$data?$_POST:$data;
		$result=$this->taskDB->add($data);
		if(false==$result){
			$this->error=$this->taskDB->getError();
			return false;
		}else{
			//$this->newProgress(['task_id'=>$result,'remark'=>'任务创建']);
			return $result;
		}
	}
	public function update($data=''){
		$data=''==$data?$_POST:$data;
		$result=$this->taskDB->where(['id'=>$data['id']])->save($data);
		if(false===$result){
			//echo $this->taskDB -> getLastSql();
			$this->error = $this->taskDB->getError();
			//echo 'xxx';
			return false;
		}else{
			//$this->newProgress(['task_id'=>$data['id'],'remark'=>session ('loginUserName').'修改任务']);
			return true;
		}
	}
	public function delete($id=''){
		$id=''==$id?$this->task_id:$id;
		
		//如果要删除的任务下面有子类，则不删除
		if($this->taskDB->where([pid=>$id])->find()){
			$this->error='存在子任务，不能删除！';
			return false;
		}
		
		$map['id']=['in',$id];
		$result=$this->taskDB->where($map)->delete();
		if(false==$result){
			$this->error=$this->taskDB->getError();
			return false;
		}else{
			return $result;
		}
	}
	
	//进度（查/改）
	public function getProgress($map=array()){
		if($map==array()){return false;}
		$info=$this->progressDB->where($map)->select();
		return $info;
	}
	
	//更改进度
	public function setProgress($data=''){
		$data=''==$data?$_POST:$data;
		$progress=$this->progressDB->where(['id'=>$data['id']])->Field('task_id,datetime,remark')->find();
		$progresstime=$this->taskDB->where(['id'=>$progress['task_id']])->getField('progresstime');
		
		//先更新源进度
		$result=$this->progressDB->where(['id'=>$data['id']])->save($data);
		if(false===$result){
			$this->error=$this->progressDB->getError();
			return false;
		}
		
		//查询是源进度是否与任务记录的进度一样，若一样则同步更改，不一样则只更改源进度
		if($progress['datetime']==$progresstime){
			$data['progresstime']=$data['datetime'];
			unset($data['id'],$data['task_id'],$data['datetime']);	
			$result2=$this->taskDB->where(['id'=>$progress['task_id']])->save($data);
			if(false===$result2){
				$this->error=$this->taskDB->getError();
				return false;	
			}
		}
		
		return $result;
	}
	
	//新建进度
	public function newProgress($data=''){
		$data=''==$data?$_POST:$data;
		$data['datetime']=empty($data['datetime'])?time():$data['datetime'];
		
		$starttime=$this->getInfo($data['task_id'],'progresstime');
		$data['starttime']=empty($starttime['progresstime'])?time():$starttime['progresstime'];
		if(!isset($data['img_url'])){
			$data['img_url'] = '/Public/timg.jpg';
		}
		//先新建源进度
		$result=$this->progressDB->add($data);
		if(false==$result){
			$this->error=$this->progressDB->getError();
			return false;
		}
		
		//再更新‘任务’的进度
		$data['progresstime']=$data['datetime'];
		$data['progress_remark']=$data['remark'];
		$task_id=$data['task_id'];
		unset($data['id'],$data['task_id'],$data['datetime'],$data['remark'],$data['starttime']);	
		$result2=$this->taskDB->where(['id'=>$task_id])->save($data);
		if(false==$result2){
			$this->error=$this->taskDB->getError();
			return false;	
		}
		
		return $result;
	}
	
	//延时 delay  特殊的进度信息
	public function delay($data=array()){
		$data=$data==array()?$_POST:$data;
		
		//先新建进度
		$result=$this->progressDB->add($data);
		if(false==$result){
			$this->error=$this->progressDB->getError();
			return false;
		}
		
		//再更新‘任务’的‘所需时间’/秒
		$result2=$this->taskDB->where(['id'=>$data['task_id']])->setInc('requiretime',$data['requiretime']);
		if(false==$result2){
			$this->error=$this->taskDB->getError();
			return false;
		}
		
		return $result;
	}
	
	//点击确认完成的方法（更改任务状态）  ->  若状态为 1/‘完成’ 那么触发相应的事件
	public function changeStatus($status=3,$id=''){
		$map['id']=''==$id?$this->task_id:$id;
		$result=$this->taskDB->where($map)->setField(['status'=>$status]);
		if(false==$result){
			$this->error=$this->taskDB->getError();
			return false;
		}else{
			return $result;
		}
	}
	
	//完成后执行的方法（或事件绑定）
	public function triggerEvent(){
		return false;	
	}
	
	
	//获取发生的错误
	public function getError(){
		return $this->error;	
	}
	
	//状态回滚  
	public function rollback($id=''){
		if($id==''){return false;}
		//只有当当前‘任务’的‘状态’为 1/‘完成’ 的时候才能回滚
		$task=$this->taskDB->where(['id'=>$id])->Field('status,integralNum')->find();
		if($task['status']!=1){return false;}
		
		//将分数与结果回滚
		
		return false;
	}
	
	
	//通知信息
	public function remindInfo(){
		return false;	
	}
}